<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\job\DownloadJob;
use app\admin\service\system\PluginService;
use app\common\service\CloudService;
use app\common\service\ConfigService;
use app\common\service\HttpService;
use think\facade\Db;
use think\Request;

use app\common\model\system\PluginModel;

/**
 * 插件（全部废弃）
 */
class Cloud extends Controller
{
    protected $cloudUrl = '';
    protected $service;

    protected $license = null;

    protected array $middleware = [
        'admin' => ['except' => ['updateSystem']],
    ];


    public function __construct()
    {
//        $this->cloudUrl = config('admin.cloud_domain');
//        $this->license = ConfigService::get('system', 'license');
//        $this->service = new CloudService();

    }

    public function check()
    {
        return success('', (new CloudService())->check());
    }

    /**
     * 检查系统是否有新版
     */
    public function checkVersion(Request $request)
    {

//        $response = HttpService::post($this->cloudUrl. '/api/cloud/checkVersion',  [
//            'headers' => [
//                'Accept' => 'application/json',
//                'license' => $this->license
//            ],
//            'json' =>  [],
//
//        ]);
//        if ($response['error']!= 0) {
//            return error($response['msg']);
//        }
//        $data = $response['data'];
//        $needs_update = needs_update(config('admin.version'), $data['version']);
//        return success('success', ['needs_update'=>$needs_update, 'latest_version' => $data['version']]);
        return success('最新');
    }

    /**
     * 更新系统
     * @param \think\Request $request
     * @return void
     */
    public function updateSystem(Request $request)
    {
//        $clientId = bin2hex(random_bytes(16));
//        $plugins = PluginModel::column('name');
//        $res = HttpService::post($this->cloudUrl . '/api/cloud/updateSystem',
//        [
//            'headers' => [
//                'Accept' => 'application/json',
//                'license' => $this->license
//            ],
//            'json' =>  ['client_id' => $clientId, 'plugins' => $plugins]
//        ]
//       );
//        return success('', ['client_id' => $clientId]);
        return success('', ['client_id' => '']);
    }

    /**
     *
     * 下载系统（后端文件）
     * @param \think\Request $request
     * @return \think\response\Json
     */
    public function chunkDownloadSystem(Request $request)
    {

//        $clientId = $request->post('client_id');
//        $chunkFiles = $request->post('chunk_files');
//        $actionType = $request->post('action_type');
//        $plugin = $request->post('plugin', []);// 插件信息
        // \think\facade\Queue::push(DownloadJob::class, ['client_id' => $clientId, 'chunk_files' => $chunkFiles, 'action_type' => $actionType, 'plugin' => $plugin], $queue = null);
//        (new \dengje\workerman\process\queue\Client())->push(DownloadJob::class, ['client_id' => $clientId, 'chunk_files' => $chunkFiles, 'action_type' => $actionType, 'plugin' => $plugin]);
//        return success('', ['file' => $chunkFiles]);
        return success('', ['file' => '']);

    }


    /**
     * 获取更新系统进程（更新系统、安装、卸载插件都用到了
     *）
     */
    public function getUpdateSystemProcess(Request $request)
    {
//        $clientId = $request->param('client_id');
//        $license = $this->license;
//        $response = HttpService::get($this->cloudUrl . '/api/cloud/getProcessLog', [
//            'headers' => [
//                'Accept' => 'application/json',
//                'license' => $license
//            ],
//            'query' => [
//                'client_id' => $clientId
//            ]
//        ]);
//        return success('success', $response['data']);
        return success('success', []);
    }

    /**
     * 我的插件列表
     */
    public function plugins(Request $request)
    {
//        $response['data'] = [];
//        if (empty($response['data'])) {
//            return success('success', []);
//        }
//        $plugins = $response['data'];
//        $installedPlugins = Db::name('plugin')->select()->toArray();
//        foreach ($plugins as $key => $plugin) {
//            foreach ($installedPlugins as $installedPlugin) {
//                $pluginInfo = app('plugin')->getInfo($plugin['name']);
//                if ($plugin['name'] == $installedPlugin['name']) {
//                    // 判断是否有配置路由
//                    if (isset($pluginInfo['settingPath'])) {
//                        $plugins[$key]['settingPath'] = $pluginInfo['settingPath'];
//                    } else {
//                        $plugins[$key]['settingPath'] = false;
//                    }
//
//                    $plugins[$key]['installed'] = true;
//                    $plugins[$key]['status'] = $installedPlugin['status'];
//                    $plugins[$key]['need_update'] = needs_update($installedPlugin['version'], $plugin['version']);
//                }
//            }
//
//
//        }
//        return success('success', $plugins);
        return success('success', []);

    }

    /**
     * 安装插件
     * @param \think\Request $request
     * @return void
     */
    public function installPlugin(Request $request, PluginService $pluginService)
    {
//        $clientId = bin2hex(random_bytes(16));
//        $pluginName = $request->param('plugin_name');
//        $version = $request->param('version');
//        // 查询以及安装的插件
//        $plugins = $plugins = PluginModel::column('name');
//        $response = HttpService::post($this->cloudUrl . '/api/cloud/installPlugin', [
//            'headers' => [
//                'Accept' => 'application/json',
//                'license' => $this->license
//            ],
//            'json' => [
//                'plugin_name' => $pluginName,
//                'version' => $version,
//                'client_id' => $clientId,
//                'plugins' => $plugins
//            ]
//        ]);
//        return success('success', $response['data']);
        return success('success', []);
    }


    /**
     *
     * 卸载插件
     * @param \think\Request $request
     * @return void
     */
    public function uninstallPlugin(Request $request, PluginService $pluginService)
    {

//        $pluginName = $request->param('plugin_name');
//        $pluginService->uninstall($pluginName);

        return success('success');
    }

    /**
     * 启用插件
     *
     */
    public function enablePlugin(Request $request, PluginService $pluginService)
    {
//        $pluginName = $request->param('plugin_name');
//        $pluginService->enable($pluginName);
        return success();
    }

    /**
     * 禁用插件
     *
     */
    public function disablePlugin(Request $request, PluginService $pluginService)
    {
//        $pluginName = $request->param('plugin_name');
//        $pluginService->disable($pluginName);
        return success();
    }


    /**
     *
     * 更新插件
     * @param \think\Request $request
     * @return void
     */
    public function updatePlugin(Request $request, PluginService $pluginService)
    {
//        $clientId = bin2hex(random_bytes(16));
//        $pluginName = $request->param('plugin_name');
//        $version = $request->param('version');
//        // $pluginService->update($pluginName, $version);
//        // 查询以及安装的插件
//        $plugins = $plugins = PluginModel::column('name');
//        $response = HttpService::post($this->cloudUrl . '/api/cloud/installPlugin', [
//            'headers' => [
//                'Accept' => 'application/json',
//                'license' => $this->license
//            ],
//            'json' => [
//                'plugin_name' => $pluginName,
//                'version' => $version,
//                'client_id' => $clientId,
//                'plugins' => $plugins
//            ]
//        ]);
//        return success(['client_id' => $clientId]);
        return success(['client_id' => '']);
    }
}